package com;

import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import model.Authority;
import model.Comment;
import model.Gallery;
import model.Order;
import model.Product;
import model.UserDetails;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class ProductDaoHibernate extends HibernateDaoSupport implements ProductDao {

	@SuppressWarnings("unchecked")
	public List<Product> getDecoys() {
		return getHibernateTemplate().find("from Product where catName=? and rentable=?", new Object[] { "decoys",false });
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> getRents() {
		return getHibernateTemplate().find("from Product where catName=? and rentable=?", new Object[] { "decoys" ,true});
	}

	@SuppressWarnings("unchecked")
	public List<Product> getCamouflages() {
		return getHibernateTemplate().find("from Product where catName=?", new Object[] { "blind" });
	}

	@SuppressWarnings("unchecked")
	public List<Comment> getComments(String param) {
		List<Comment> comments = getHibernateTemplate().find("from Comment where productName=?", new Object[] { param });
		Collections.reverse(comments);
		return comments;
	}

	public void save(Order order) {
		getHibernateTemplate().save(order);
	}

	public void save(Comment comment) {
		getHibernateTemplate().save(comment);

	}

	@SuppressWarnings("unchecked")
	public List<Product> search(String param) {
		String query = "from Product p where p.name like :name or p.catName like :catName";
		return getHibernateTemplate().findByNamedParam(query, new String[] { "name", "catName" },
				new Object[] { '%' + param + '%', '%' + param + '%' });
	}

	@SuppressWarnings("unchecked")
	public List<Gallery> getGallery(String param) {
		return getHibernateTemplate().find("from Gallery where productId=?", new Object[] { param });
	}

	@SuppressWarnings("unchecked")
	public Product getProductById(String param) {
		return (Product) DataAccessUtils.uniqueResult(getHibernateTemplate().find("from Product where id=?",
				new Object[] { param }));
	}

	public void registerNewUser(UserDetails user) {
		Set<Authority> auth = new HashSet<Authority>();
		Authority a = new Authority();
		a.setAuthority("ROLE_USER");
		a.setUsername(user.getUsername());
		auth.add(a);
		user.setAuthorities(auth);
		user.setConfirmationCode(String.valueOf((new Date().getTime() + 42)));
		getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	public String getUserCity(String username) {
		return (String) DataAccessUtils.uniqueResult(getHibernateTemplate().find(
				"select city from UserDetails where username=?", new Object[] { username }));
	}

	@SuppressWarnings("unchecked")
	public String getUserMail(String username) {
		return (String) DataAccessUtils.uniqueResult(getHibernateTemplate().find(
				"select mail from UserDetails where username=?", new Object[] { username }));
	}

	public int confirmUser(String username, String confirmationCode) {
		SessionFactory sf = getHibernateTemplate().getSessionFactory();
		Session s = sf.openSession();
		Query q = s
				.createQuery("update UserDetails set confirmed=true where username=:username and confirmationCode=:confirmationCode");
		q.setString("username", username);
		q.setString("confirmationCode", confirmationCode);
		return q.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public boolean confirmed(String username) {
		return (Boolean) DataAccessUtils.uniqueResult(getHibernateTemplate().find(
				"select confirmed from UserDetails where username=?", new Object[] { username }));
	}

}
